package com.beiing.tianshuai.tianshuai.interest.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.interest.entity.InterestVideoDetailBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Jinni on 2017/12/3 0003
 */

public class InterestVideoDetailModel implements InterestVideoDetailModelImpl {

    private static final String TAG = "InterestVideoDetailMode";
    private OnInterestVideoDetailResponseListener mListener;

    public InterestVideoDetailModel(OnInterestVideoDetailResponseListener listener) {
        mListener = listener;
    }

    @Override
    public void getVideoDetail(String vid, String lastId, String pageSize, int pageNum, String uid, int code) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("vid", vid)
                .addFormDataPart("lastId", lastId)
                .addFormDataPart("pageSize", pageSize)
                .addFormDataPart("page", String.valueOf(pageNum))
                .addFormDataPart("uid", uid)
                .addFormDataPart("code", String.valueOf(code))
                .build();
        Observable<InterestVideoDetailBean> observable = DataManager.getInterestDetail(body);
        Observable<InterestVideoDetailBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<InterestVideoDetailBean>() {
            @Override
            public void onNext(InterestVideoDetailBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getMoreReply(String vid, String lastCid, String pageSize, int pageNum, String uid, String lastRid, int code) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("vid", vid)
                .addFormDataPart("lastId", lastCid)
                .addFormDataPart("lastRid", lastRid)
                .addFormDataPart("code", String.valueOf(code))
                .addFormDataPart("pageSize", pageSize)
                .addFormDataPart("uid", uid)
                .addFormDataPart("page", String.valueOf(pageNum))
                .build();
        Observable<InterestVideoDetailBean> observable = DataManager.getInterestDetail(body);
        Observable<InterestVideoDetailBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<InterestVideoDetailBean>() {
            @Override
            public void onNext(InterestVideoDetailBean bean) {
                if (mListener != null) {
                    mListener.onGetMoreReplySuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetMoreReplyError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void submitReply(String vid, String uid, String toUid, String content) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("vid", vid)
                .addFormDataPart("uid", uid)
                .addFormDataPart("to_uid", toUid)
                .addFormDataPart("content", content)
                .build();
        Observable<CodeBean> observable = DataManager.getAddVideoReplyResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
                if (mListener != null) {
                    mListener.onAddReplySuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onAddReplyError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getPraiseResult(String vid, String uid, String controller) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("vid", vid)
                .addFormDataPart("uid", uid)
                .addFormDataPart("controller", controller)
                .build();
        Observable<CodeBean> observable = DataManager.getPriseResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void setFollowed(String uid, String toUid) {
        Observable<FollowResultBean> result = DataManager.getFollowResult(
                uid,
                toUid
        );
        Observable<FollowResultBean> signUpBeanObservable = result.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<FollowResultBean>() {

            @Override
            public void onNext(@NonNull FollowResultBean bean) {
                if (mListener != null) {
                    mListener.onFollowedSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onFollowedError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void setPraiseReply(String id, String vid, String uid, String toUid) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("cid", id)
                .addFormDataPart("vid", vid)
                .addFormDataPart("uid", uid)
                .addFormDataPart("to_uid", toUid)
                .build();
        Observable<CodeBean> observable = DataManager.getReplyPriseResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void deleteReply(String cid, String uid, final int position, String type) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("id", cid)
                .addFormDataPart("uid", uid)
                .addFormDataPart("type", type)
                .build();
        Observable<CodeBean> observable = DataManager.deleteVideoReplyResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
                if (mListener != null) {
                    mListener.onDelReplySuccess(bean, position);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    mListener.onDelReplyError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void addCommentReply(String vid, String repliedRId, String uid, String toUid, String content, String originalContent, String replyType, String repliedCId) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("vid", vid)
                .addFormDataPart("rid", repliedRId)
                .addFormDataPart("cid", repliedCId)
                .addFormDataPart("content", content)
                .addFormDataPart("comment", originalContent)
                .addFormDataPart("uid", uid)
                .addFormDataPart("to_uid", toUid)
                .addFormDataPart("type", replyType)
                .build();
        Observable<CodeBean> observable = DataManager.addCommentReply(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
                if (mListener != null) {
                    mListener.onAddReplySuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    mListener.onAddReplyError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void setPraiseCommentReply(String cid, String vid, String uid, String toUid, String rid) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("cid", cid)
                .addFormDataPart("vid", vid)
                .addFormDataPart("uid", uid)
                .addFormDataPart("to_uid", toUid)
                .addFormDataPart("rid", rid)
                .build();
        Observable<CodeBean> observable = DataManager.getCommentReplyPriseResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public interface OnInterestVideoDetailResponseListener {
        void onGetDetailSuccess(InterestVideoDetailBean bean);

        void onGetDetailError(Throwable throwable);

        void onGetMoreReplySuccess(InterestVideoDetailBean bean);

        void onGetMoreReplyError(Throwable throwable);

        void onAddReplySuccess(CodeBean bean);

        void onAddReplyError(Throwable throwable);

        void onFollowedSuccess(FollowResultBean bean);

        void onFollowedError(Throwable throwable);

        void onDelReplySuccess(CodeBean bean, int position);

        void onDelReplyError(Throwable throwable);
    }
}
